package com.bjpowernode.crm.workbench.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.bjpowernode.crm.setting.eneity.User;
import com.bjpowernode.crm.util.DateTimeUtil;
import com.bjpowernode.crm.util.UUIDUtil;
import com.bjpowernode.crm.vo.PageinationVO;
import com.bjpowernode.crm.workbench.eneity.*;
import com.bjpowernode.crm.workbench.exception.TranException;
import com.bjpowernode.crm.workbench.service.ActivityService;
import com.bjpowernode.crm.workbench.service.ContactsService;
import com.bjpowernode.crm.workbench.service.CustomerService;
import com.bjpowernode.crm.workbench.service.TranService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
public class TranController {
    @Autowired
    private TranService tranService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ContactsService contactsService;
    @Autowired
    private CustomerService customerService;
    //分页查询
    @RequestMapping(value = "/workbench/transaction/pageList.do")
    @ResponseBody
    public PageinationVO<Tran> pageList(@RequestParam Map<String,String> params){
        //完善请求参数
        String pageNo=params.get("pageNo");
        if (pageNo==null||"".equals(pageNo)){
            pageNo="1";
            params.put("pageNo",pageNo);
        }
        //调用service
        PageinationVO<Tran> vo=tranService.pageList(params);
        return vo;

    }
    @RequestMapping(value = "/workbench/transaction/getUserList.do")
    public ModelAndView getUserList(){
        ModelAndView model=new ModelAndView();
        //调用service
        List<User> userList=tranService.getUserList();
        //将结果写入model进行转发
        model.addObject("userList",userList);
        model.setViewName("forward:/workbench/transaction/save.jsp");
        return model;
    }
    @RequestMapping(value = "/workbench/transaction/getActivityList.do")
    @ResponseBody
    public List<Activity> getActivityList(){
        //调用service
        List<Activity> activityList=activityService.getActivityList();
        return activityList;
    }
    @RequestMapping(value = "/workbench/transaction/searchActivityByName.do")
    @ResponseBody
    public List<Activity> getActivityListByName(String name){
        //调用service
        List<Activity> activityList=activityService.getActivityByName(name);
        return activityList;
    }
    @RequestMapping(value = "/workbench/transaction/getContactsList.do")
    @ResponseBody
    public List<Contacts> getContactsList(@RequestParam String contactsName){
        //调用service
        List<Contacts> contactsList=contactsService.getContactsList(contactsName);
        return contactsList;
    }
    @RequestMapping(value = "/workbench/transaction/getCustomerName.do")
    @ResponseBody
    public List<String> getCustomerNameList(String name){
        List<String> customerNameList=customerService.getCustomerNameList(name);
        return customerNameList;
    }
    @RequestMapping(value = "/workbench/transaction/save.do")
    public ModelAndView save(@RequestParam Map<String,String> params, HttpServletRequest request){
        //完善交易参数信息
        params.put("id", UUIDUtil.getUUID());
        HttpSession session=request.getSession(false);
        User user= (User) session.getAttribute("loginUser");
        params.put("createBy",user.getName());
        params.put("createTime", DateTimeUtil.getSysTime());
        //调用service
        ModelAndView model=new ModelAndView();
        try{
            boolean success=tranService.save(params);
            model.setViewName("redirect:/workbench/transaction/index.jsp");
        }catch (TranException ex){
           ex.printStackTrace();
        }
        return model;
    }
    @RequestMapping(value ="/workbench/transaction/detail.do" )
    @ResponseBody
    public ModelAndView detail(@RequestParam(value = "id") String id,HttpServletRequest request){
        //调用service
        Map<String,Object> detailMap=tranService.getTranById(id);
        //进行转发
        ModelAndView model=new ModelAndView();
        Set<String> set= detailMap.keySet();
        for (String key:set) {
            model.addObject(key, detailMap.get(key));
        }
        model.setViewName("forward:/workbench/transaction/detail.jsp");
        return model;
    }
    @RequestMapping(value = "/workbench/transaction/getTranHistoryList.do")
    @ResponseBody
    public List<TranHistory> getHistoryList(@RequestParam(value = "tranId") String tranId){
        System.out.println("tranId======"+tranId);
        //调用service层
        List<TranHistory> historyList=tranService.getTranHistoryList(tranId);
        return historyList;
    }
    @RequestMapping(value = "/workbench/transaction/changeStage.do")
    @ResponseBody
    public Map<String,Object> changeStage(@RequestParam Map<String,String> params,HttpServletRequest request){
        //完善可能性信息
        User user= (User) request.getSession(false).getAttribute("loginUser");
        String jsonStr= (String) request.getServletContext().getAttribute("pMap");
        JSONObject json=JSONObject.parseObject(jsonStr);
        String possibility=json.getString(params.get("stage"));
        //交易变更的信息
        Tran tran=new Tran();
        tran.setEditBy(user.getName());
        tran.setEditTime(DateTimeUtil.getSysTime());
        params.put("editBy",user.getName());
        params.put("editTime",DateTimeUtil.getSysTime());
        params.put("createBy",user.getName());
        params.put("createTime",DateTimeUtil.getSysTime());
        //调用service
        Map<String,Object> map=new HashMap<>();
        Boolean success=false;
        try{
            success=tranService.changeStage(params);
            map.put("possibility",possibility);
            map.put("tran",tran);
        }catch (TranException ex){
            ex.printStackTrace();
        }finally {
            map.put("success",success);
            return map;
        }
    }
    @RequestMapping(value = "/workbench/tran/getTranStageAndCount.do")
    @ResponseBody
    public PageinationVO<Map<String,String>> getTranStageAndCount(){
        //调用service
        PageinationVO<Map<String,String>> vo=tranService.getTranStageAndCount();
        return vo;
    }
}
